$(document).ready(function () {
    var groupNewsUrl = global.localhost+"Api/New/first_jtxw_three"
    sendAjax(groupNewsUrl,function (res) {
        console.log(res);
        var dateTime = res[0].date.split(".");
        var html01 = '';
        html01 += '<a href="news-details.html?item=0&id='+res[0].id+'">';
        html01 += '<p class="time">'+dateTime[0]+'年'+dateTime[1]+'月'+dateTime[2]+'日</p>';
        html01 += '<p class="title">'+res[0].title+'</p>';
        html01 += '<p class="content">'+res[0].sub_content+'</p>';
        html01 += '</a>';
        var html02 = "";
        for(var i=1;i<res.length;i++){
            var dataTime = res[i].date.split(".");
            html02 += '<li><a href="news-details.html?item=0&id='+res[i].id+'"><span class="title">'+res[i].title+'</span><span class="time">'+dataTime[0]+'年'+dataTime[1]+'月'+dataTime[2]+'日</span></a></li>'
        }
        // $(".jtyw .news-first").html(html01);
        // $(".news-list").html(html02);
    })
    var mediaUrl = global.localhost+"Api/New/first_hydt_three";
    sendAjax(mediaUrl,function (res) {
        // var dateTime = res[0].date.split(".");
        // var html01 = "";
        // html01 += '<a href="news-details.html?item=0&id='+res[0].id+'">';
        // html01 += '<p class="time">'+dateTime[0]+'年'+dateTime[1]+'月'+dateTime[2]+'日</p>';
        // html01 += '<p class="title">'+res[0].title+'</p>';
        // html01 += '<p class="content">'+res[0].sub_content+'</p>';
        // html01 += '</a>';
        // var html02 = "";
        // for(var i=1;i<2;i++){
        //     var dateTime = res[i].date.split(".");
        //     html02 += '<li><a href="news-details.html?item=0&id='+res[i].id+'"><span class="title">'+res[i].title+'</span><span class="time">'+dataTime[0]+'年'+dataTime[1]+'月'+dataTime[2]+'日</span></a></li>'
        // }
        // $(".news-page-headline").html(html01);
        // $(".news-tj").html(html02);
    })
    /*
     *old page js begin
     */
    var getGroupNews = global.localhost+"Api/New/get_jtxw";
    var getMediaNews = global.localhost+"Api/New/get_hydt";
    var current = 1;
    var mediaCurrent = 1;
    var page = {p:current};
    var mediaPage = {p:mediaCurrent};
    var sum = null;
    var mediaSum = null;
    var callback = function(res){
        var html = "";
        for(var i=0;i<res.list.length;i++){
            var datetime = res.list[i].date.split(".");
            html += '<li class="news-item">';
            html += '<a href="news-details.html?item=0&id='+res.list[i].id+'">';
            html += '<div class="news-infro">';
            html += '<p class="news-title">'+res.list[i].title+'</p>';
            html += '<p class="content">'+res.list[i].sub_content+'</p>';
            html += '</div>';
            html += '<div class="news-date">'+datetime[0]+'年'+datetime[1]+'月'+datetime[2]+'日</div>';
            html += '</a>';
            html += '</li>';
        }
        sum = res.sum;
        // $(".jtyw ul").html(html);
        //计算页数
        $(".total").text("总共"+sum+"页");
        $(".current").text("当前第"+current+"页/"+sum+"页");
    }
    sendAjax(getGroupNews,function (res) {
       callback(res)
    },page)
    $(".jt-next").on("click",function () {
        // if(current<)
        console.log(sum);
        if(current<sum){
            current++;
            sendAjax(getGroupNews,function (res) {
                callback(res)
            },page);
        }else{
            alert("本页已是新闻最后一页！")
        }
    })
    $(".jt-prev").on("click",function () {
        if(current>1){
            current--;
            sendAjax(getGroupNews,function (res) {
                callback(res)
            },page)
        }else{
            alert("本页已是新闻首页！！")
        }
    })
    $(".jt-first-page").on("click",function () {
        current = 1;
        sendAjax(getGroupNews,function (res) {
            callback(res)
        },page)
        console.log("first");
    })
    $(".jt-last-page").on("click",function () {
        current = sum;
        sendAjax(getGroupNews,function (res) {
            callback(res)
        },page)
        console.log("last");
    })
    //媒体报道
    var mediaCallback = function (res) {
        var html = "";
        for(var i=0;i<res.list.length;i++){
            var datetime = res.list[i].date.split(".");
            html += '<li class="news-item">';
            html += '<a href="news-details.html?item=1&id='+res.list[i].id+'">';
            html += '<div class="news-infro">';
            html += '<p class="news-title">'+res.list[i].title+'</p>';
            html += '<p class="content">'+res.list[i].sub_content+'</p>';
            html += '</div>';
            html += '<div class="news-date">'+datetime[0]+'年'+datetime[1]+'月'+datetime[2]+'日</div>';
            html += '</a>';
            html += '</li>';
        }
        mediaSum = res.sum;
        $(".mtbd ul").html(html);
        //计算页数
        $(".mt-total").text("总共"+mediaSum+"页");
        $(".mt-current").text("当前第"+mediaCurrent+"页/"+mediaSum+"页");
    }
    sendAjax(getMediaNews,function (res) {
        mediaCallback(res)
    },mediaPage)
    $(".next").on("click",function () {
        // if(current<)
        console.log(mediaSum);
        if(mediaCurrent<mediaSum){
            mediaCurrent++;
            sendAjax(getMediaNews,function (res) {
                mediaCallback(res)
            },mediaPage);
        }else{
            alert("本页已是新闻最后一页！")
        }
    })
    $(".prev").on("click",function () {
        if(mediaCurrent>1){
            mediaCurrent--;
            sendAjax(getMediaNews,function (res) {
                mediaCallback(res)
            },mediaPage)
        }else{
            alert("本页已是新闻首页！！")
        }
    })
    $(".first-page").on("click",function () {
        mediaCurrent = 1;
        sendAjax(getMediaNews,function (res) {
            mediaCallback(res)
        },mediaPage)
        console.log("first");
    })
    $(".last-page").on("click",function () {
        mediaCurrent = mediaSum;
        sendAjax(getMediaNews,function (res) {
            mediaCallback(res)
        },mediaPage)
        console.log("last");
    })
    /*
     * old page end
     */
    var pageItem = getSearch();
    pageItem = pageItem[0].split('=')[1];
    pageItem = pageItem==1?1:0;
    $('.xinwen').css('display','none');
    $('.xinwen').eq(pageItem).css('display','block');
    $('.nav ul').on("click","li",function () {
        var index = $(this).index();
        // $('.xinwen').css('display','none');
        // $('.xinwen').eq(index).css('display','block')
        location.href = 'news.html?item='+index;
    })
    var jtywUrl = global.localhost + 'Api/New/get_jtxw';
    var mtbdUrl = global.localhost + 'Api/New/get_hydt';
    var page = 1;
    var p = {p:page};
    sendAjax(jtywUrl,function(res){
        if(res.list.length == 0 ){
            $('.jtyw-new .no-news').css('display','block');
            $('.jtyw-new .getMore').css('display','none');
        }else{
            var html = '<a href="news-details-bak.html?item=0&id='+res.list[0].id+'">';
            html += '<div class="first-img" style="background: url('+res.list[0].title_img+') center center/cover no-repeat"></div>';
            html += '<div class="first-title"><span class="first-date">'+res.list[0].date+'</span>'+res.list[0].title+'</div>';
            html += '<div class="first-sub">'+res.list[0].sub_content+'</div>';
            html += '<div class="goto-details">头条详情</div>';
            html += '</a>'
            $('.jtyw-new .news-first').html(html);
            var newsItems = '';
            for(var i=1;i<res.list.length;i++){
                newsItems += '<li>'
                newsItems += '<a href="news-details-bak.html?item=0&id='+res.list[i].id+'">';
                newsItems += '<div class="items-date">'+res.list[i].date+'</div>';
                newsItems += '<div class="items-main">';
                newsItems += '<p class="items-title">'+res.list[i].title+'</p>';
                newsItems += '<p class="items-sub-content">'+res.list[i].sub_content+'</p>';
                newsItems += '</div>';
                newsItems += '</a>';
                newsItems += '</li>'
            }
            $('.jtyw-new .news-items').html(newsItems);
            if(res.ok==0){
                $('.jtyw-new .getMore').css('display','none');
            }
        }
    },p);
    $('.jtyw-new').on('click','.getMore',function () {
        page = page+1;
        p = {p:page};
        sendAjax(jtywUrl,function (res) {
            console.log(res);

            var newsItems = '';
            for(var i=0;i<res.list.length;i++){
                newsItems += '<li>'
                newsItems += '<a href="news-details-bak.html?item=1&id='+res.list[i].id+'">';
                newsItems += '<div class="items-date">'+res.list[i].date+'</div>';
                newsItems += '<div class="items-main">';
                newsItems += '<p class="items-title">'+res.list[i].title+'</p>';
                newsItems += '<p class="items-sub-content">'+res.list[i].sub_content+'</p>';
                newsItems += '</div>';
                newsItems += '</a>';
                newsItems += '</li>'
            }
            $('.jtyw-new .news-items').append(newsItems);
            if(res.ok==0){
                $('.jtyw-new .getMore').css('display','none');
            }
        },p)
    })
    var page01 = 1;
    var p01 = {p:page01};
    sendAjax(mtbdUrl,function (res) {
        if(res.list.length == 0){
            // alert('暂无新闻');
            $('.mtbd-new .no-news').css('display','block');
            $('.mtbd-new .getMore').css('display','none');
        }else{
            var html = '<a href="news-details-bak.html?item=0&id='+res.list[0].id+'">';
            html += '<div class="first-img" style="background: url('+res.list[0].title_img+') center center/cover no-repeat"></div>';
            html += '<div class="first-title"><span class="first-date">'+res.list[0].date+'</span>'+res.list[0].title+'</div>';
            html += '<div class="first-sub">'+res.list[0].sub_content+'</div>';
            html += '<div class="goto-details">头条详情</div>';
            html += '</a>'
            $('.mtbd-new .news-first').html(html);
            var newsItems = '';
            for(var i=1;i<res.list.length;i++){
                newsItems += '<li>'
                newsItems += '<a href="news-details-bak.html?item=0&id='+res.list[i].id+'">';
                newsItems += '<div class="items-date">'+res.list[i].date+'</div>';
                newsItems += '<div class="items-main">';
                newsItems += '<p class="items-title">'+res.list[i].title+'</p>';
                newsItems += '<p class="items-sub-content">'+res.list[i].sub_content+'</p>';
                newsItems += '</div>';
                newsItems += '</a>';
                newsItems += '</li>'
            }
            $('.mtbd-new .news-items').html(newsItems);
            if(res.ok==0){
                $('.mtbd-new .getMore').css('display','none');
            }
        }
    },p01);
    $('.mtbd-new').on('click','.getMore',function () {
        page01 = page+1;
        p01 = {p:page01};
        sendAjax(jtywUrl,function (res) {
            var newsItems = '';
            for(var i=0;i<res.list.length;i++){
                newsItems += '<li>'
                newsItems += '<a href="news-details-bak.html?item=0&id='+res.list[i].id+'">';
                newsItems += '<div class="items-date">'+res.list[i].date+'</div>';
                newsItems += '<div class="items-main">';
                newsItems += '<p class="items-title">'+res.list[i].title+'</p>';
                newsItems += '<p class="items-sub-content">'+res.list[i].sub_content+'</p>';
                newsItems += '</div>';
                newsItems += '</a>';
                newsItems += '</li>'
            }
            $('.jtyw-new .news-items').append(newsItems);
            if(res.ok==0){
                $('.jtyw-new .getMore').css('display','none');
            }
        },p01)
    })



    var tjUrl = global.localhost + 'Api/New/get_tj';
    sendAjax(tjUrl,function (res) {
        console.log(res);
        var jtxwTj = '<a href="news-details-bak.html?item=0&id='+res.jtxw.id+'">'
        jtxwTj += '<div class="recommend-img" style="background: url('+res.jtxw.hot_img+') no-repeat center center/cover"></div>';
        jtxwTj += '<p class="recommend-title">'+res.jtxw.title+'</p>';
        jtxwTj += '<p class="recommend-sub-content">'+res.jtxw.sub_content+'</p>';
        jtxwTj += '</a>'
        $('.jtyw-new .recommend').html(jtxwTj);
        var mtbdTj = '<a href="news-details-bak.html?item=0&id='+res.hydt.id+'">'
        mtbdTj = '<div class="recommend-img" style="background: url('+res.hydt.hot_img+') no-repeat center center/cover"></div>';
        mtbdTj += '<p class="recommend-title">'+res.hydt.title+'</p>';
        mtbdTj += '<p class="recommend-sub-content">'+res.hydt.sub_content+'</p>';
        mtbdTj +='</a>'
        $('.mtbd-new .recommend').html(mtbdTj)
    })
})
